/*
 * main.cpp
 *
 *  Created on: 14.10.2011
 *      Author: dmitriy
 */

#include <iostream>
#include <cstring>
#include <algorithm>

struct CArray {

	CArray(size_t size) :
			m_vector(0), m_size(size), m_count(size) {
		m_vector = new int[size];
	}

	CArray(const CArray& arr) :
			m_size(arr.m_size), m_count(arr.m_count) {
		m_vector = new int[m_size];
		memmove(m_vector, arr.m_vector, sizeof(int) * m_size);
	}

	CArray&
	operator=(const CArray& arr) {
		CArray tmp(arr);
		swap(tmp);
		return *this;
	}

	~CArray() {
		delete[] m_vector;
	}

	void set(size_t pos, int val) {
		m_vector[pos] = val;
	}

	int get(size_t pos) const {
		return m_vector[pos];
	}

	void push_back(int val) {
		if (m_count < m_size) {
			m_vector[m_count] = val;
			++m_count;
		} else {
			realloc();
			m_vector[m_count] = val;
			++m_count;
		}
	}

	size_t size() const {
		return m_count;
	}

	void swap(CArray& arr) {
		std::swap(m_size, arr.m_size);
		std::swap(m_count, arr.m_count);
		std::swap(m_vector, arr.m_vector);
	}

private:
	int* m_vector;
	size_t m_size;
	size_t m_count;

	void free() {
		if (m_size)
			delete[] m_vector;
		m_size = 0;
	}

	void realloc() {
		size_t new_size = m_size * 2;
		int* vector = new int[new_size];
		memmove(vector, m_vector, m_size * sizeof(int));
		free();
		m_vector = vector;
		m_size = new_size;
	}
};

int main() {
	CArray a(10);

	for (size_t i = 0; i < 10; ++i)
		a.set(i, i);

	for (size_t i = 10; i < 20; ++i)
		a.push_back(i);

	for (size_t i = 0; i < a.size(); ++i)
		std::cout << a.get(i) << " ";

	std::cout << std::endl;
	return 0;
}
